package com.example.idea.controller;

import com.example.idea.entity.Notice;
import com.example.idea.service.NoticeService;
import com.example.idea.utils.HoneyLogs;
import com.example.idea.utils.LogType;
import com.example.idea.utils.Page;
import com.example.idea.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 添加公告
     */
    @HoneyLogs(operation = "新增",type = LogType.ADD)
    @RequestMapping("/add")
    public Result add(@RequestBody Notice notice) {
        noticeService.add(notice);
        return Result.success(notice);
    }

    /**
     * 修改公告
     */
    @HoneyLogs(operation = "新增",type = LogType.UPDATE)
    @RequestMapping("/update")
    public Result update(@RequestBody Notice notice) {
        log.info("修改用户:{}", notice);
        noticeService.update(notice);
        return Result.success(notice);
    }

    /**
     * 删除公告
     */
    @HoneyLogs(operation = "新增",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        noticeService.delete(id);
        return Result.success(null);
    }

    /**
     * 根据ID查询公告
     */
    @RequestMapping("/query/{id}")
    public Result query(@RequestParam Integer id) {
        Notice notice = noticeService.query(id);
        return Result.success(notice);
    }

    /**
     * 批量删除公告语句
     */
    @HoneyLogs(operation = "新增",type = LogType.DELETE_ALL)
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        noticeService.deleteBatch(ids);
        return Result.success(null);
    }

    /**
     * 查询全部公告信息
     */
    @RequestMapping("/list")
    public Result list() {
        log.info("查询全部用户信息");
        List<Notice> notices = noticeService.list();
        return Result.success(notices);
    }

    /**
     * 根据不同条件进行查询数据
     */
    @RequestMapping("/selectList")
    public Result list(@RequestBody Notice notice) {
        List<Notice> notices = noticeService.selectList(notice);
        return Result.success(notices);
    }

    @RequestMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String title,
                               @RequestParam String time) {
        log.info("分页查询，页码：{},页大小：{},用户名：{},姓名：{}", pageNum, pageSize, title, time);
        Page<Notice> page = noticeService.selectByPage(pageNum, pageSize, title, time);
        return Result.success(page);
    }
}
